let Promise = require('./promise');
let fs = require('fs');

let p = new Promise(function (resolve, reject) {
    reject('123')

});


p.then().then().then(1,err=>{
    console.log('err:',err);
});

/**
 * bluebird  mz
 * node 中 util模块自带了这个功能(promisify)
 * @param fn
 * @returns {function(...[*]): Promise}
 */
function promisify(fn) {
  return function (...args) {
    return new Promise((resolve,reject)=>{
      fn(...args,function (err, data) {
        if (err) reject(err);
        resolve(data);
      });
    })
  }
}

let readFile = promisify(fs.readFile);

Promise.all([1,readFile('./2.promise/name.txt'),readFile('./2.promise/age.txt')])
  .then(data=>{
    console.log(data);
  },err=>{
    console.log(err);

  })
